package com.sign.fragment;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.biz.service.AttendanceService;
import com.biz.service.UserService;
import com.core.entity.Location;
import com.sign.signsystem.LocationApplication;
import com.sign.signsystem.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SignFragment extends HandlerFragment {

	public LocationClient mLocationClient = null;
	TextView mTextView, mTextViewResult;
	ListView listLessonListView;

	public void onCreate() {
		mLocationClient = ((LocationApplication) getActivity().getApplication()).mLocationClient;

		// 声明LocationClient类
	}

	public static SignFragment newInstance() {
		SignFragment fragment = new SignFragment();
		// Bundle args = new Bundle();
		// args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		// fragment.setArguments(args);
		return fragment;
	}

	public SignFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mLocationClient = ((LocationApplication) getActivity().getApplication()).mLocationClient;
		View rootView = inflater.inflate(R.layout.sign_fragment, container,
				false);
		listLessonListView = (ListView) rootView
				.findViewById(R.id.lesson_attendance_list);
		ArrayAdapter adapter = new ArrayAdapter(rootView.getContext(),
				R.layout.list_item_attendance, R.id.attendance_item,
				new AttendanceService().getFitMapForAdapter(new Location()));
		listLessonListView.setAdapter(adapter);
		listLessonListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> paramAdapterView,
					View paramView, int paramInt, long paramLong) {
				dialog(paramInt, paramLong);
			}

		});
		return rootView;
	}

	private void InitLocation() {
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
		mLocationClient.setLocOption(option);
	}

	@Override
	public void onStop() {
		mLocationClient.stop();
		super.onStop();
	}

	protected void dialog(int paramInt, long paramLong) {
		AlertDialog.Builder builder = new Builder(getView().getContext());
		builder.setMessage("确认签到？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						new Thread(new Runnable() {
							public void run() {
								Message message = new Message();
								if (new AttendanceService()
										.signByStudent(new Location())) {
									message.what = 8;
								} else {
									message.what = 16;
								}
								mHandler.sendMessage(message);
							}
						}).start();
						dialog.dismiss();
					}
				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	@Override
	void hanldeMessage(Message msg) {
		switch (msg.what) {
		case 8:
			showLongMessage("签到成功！");
			break;
		case 16:
			showLongMessage("签到失败！");
			break;
		default:
			showLongMessage("网络超时！");
			break;
		}
	}

}
